<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <!-- 
        方案一：使用计算属性
        方案二：使用自定义指令
       -->
      <h2>vue3代替过滤器的方案</h2>
      <p>sex: {{ sex2 }}</p>
      <p>当前时间: {{ nowTime }}</p>

      <ul>
        <li v-for="item in arr" :key="item.id" v-filter="item.time"></li>
      </ul>
    </div>

    <script src="./lib/Vue2.js"></script>
    <script>
      new Vue({
        el: "#app",
        data: {
          sex: 1,
          time: 1677490309959,
          arr: [
            {
              id: 1,
              time: 1677490309959,
            },
            {
              id: 2,
              time: 1677490559959,
            },
          ],
        },
        computed: {
          sex2() {
            return this.sex === 1 ? "男" : "女";
          },
          nowTime() {
            var date = new Date(this.time);
            var Y = date.getFullYear() + "-";
            var M =
              (date.getMonth() + 1 < 10
                ? "0" + (date.getMonth() + 1)
                : date.getMonth() + 1) + "-";
            var D =
              (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) +
              " ";
            var h =
              (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) +
              ":";
            var m =
              (date.getMinutes() < 10
                ? "0" + date.getMinutes()
                : date.getMinutes()) + ":";
            var s =
              date.getSeconds() < 10
                ? "0" + date.getSeconds()
                : date.getSeconds();
            return Y + M + D + h + m + s;
          },
        },
        directives: {
          filter(el, binding) {
            var date = new Date(binding.value);
            var Y = date.getFullYear() + "-";
            var M =
              (date.getMonth() + 1 < 10
                ? "0" + (date.getMonth() + 1)
                : date.getMonth() + 1) + "-";
            var D =
              (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) +
              " ";
            var h =
              (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) +
              ":";
            var m =
              (date.getMinutes() < 10
                ? "0" + date.getMinutes()
                : date.getMinutes()) + ":";
            var s =
              date.getSeconds() < 10
                ? "0" + date.getSeconds()
                : date.getSeconds();
            el.innerHTML = Y + M + D + h + m + s;
          },
        },
      });
    </script>
  </body>
  <html></html>
</html>
